package team.kirohuji.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import team.kirohuji.domain.user.Role;
import team.kirohuji.repository.jpa.user.RoleRepository;
import team.kirohuji.service.user.RoleService;

@RestController
@RequestMapping("/role")
public class RoleController {

	@Resource
	private RoleService roleService;
	@Autowired
	private RoleRepository roleRepository;

	/**
	 * Saves a given entity. Use the returned instance for further operations as the
	 * save operation might have changed the entity instance completely.
	 * 
	 * @param entity
	 * @return the saved entity
	 */
	@RequestMapping(value = "", method = RequestMethod.POST)
	Role save(@RequestBody Role entity) {
		return roleRepository.save(entity);
	}

	/**
	 * Returns the number of entities available.
	 * 
	 * @return the number of entities
	 */
	@RequestMapping(value = "/count", method = RequestMethod.GET)
	long count() {
		return roleRepository.count();
	}

	/**
	 * Deletes the entity with the given id.
	 * 
	 * @param id
	 *            must not be {@literal null}.
	 * @throws IllegalArgumentException
	 *             in case the given {@code id} is {@literal null}
	 */
	@RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
	void delete(@PathVariable Integer id) {
		roleRepository.deleteById(id);
	}

	/**
	 * Retrieves an entity by its id.
	 * 
	 * @param id
	 *            must not be {@literal null}.
	 * @return the entity with the given id or {@literal null} if none found
	 * @throws IllegalArgumentException
	 *             if {@code id} is {@literal null}
	 */
	@RequestMapping(value = "/{id}", method = RequestMethod.GET)
	Role findOne(@PathVariable Integer id) {
		return roleRepository.getOne(id);
	}

	/**
	 * Deletes all entities managed by the roleRepository. 根据repository 删除所有的记录
	 */
	@RequestMapping(value = "/all", method = RequestMethod.DELETE)
	void deleteAll() {
		roleRepository.deleteAll();
	}

	/**
	 * Saves all given entities.
	 * 
	 * @param entities
	 * @return the saved entities
	 * @throws IllegalArgumentException
	 *             in case the given entity is (@literal null}.
	 */
	@RequestMapping(value = "/all", method = RequestMethod.POST)
	List<Role> save(@RequestBody List<Role> entities) {
		return roleRepository.saveAll(entities);
	}

	/**
	 * Returns all instances of the type.
	 * 
	 * @return all entities
	 */
	@RequestMapping(value = "/all", method = RequestMethod.GET)
	List<Role> findAll() {
		return roleRepository.findAll();
	}

	/**
	 * Returns whether an entity with the given id exists.
	 * 
	 * @param id
	 *            must not be {@literal null}.
	 * @return true if an entity with the given id exists, {@literal false}
	 *         otherwise
	 * @throws IllegalArgumentException
	 *             if {@code id} is {@literal null}
	 */
	@RequestMapping(value = "/exists/{id}", method = RequestMethod.GET)
	boolean exists(@PathVariable Integer id) {
		return roleRepository.existsById(id);
	}

	/**
	 * Returns all instances of the type with the given IDs.
	 * 
	 * @param ids
	 * @return
	 */
	@RequestMapping(value = "/g/{ids}", method = RequestMethod.GET)
	List<Role> findAll(@PathVariable String ids) {
		String[] register = ids.split(";");
		List<String> strList = Arrays.asList(register);
		List<Integer> intList = new ArrayList<Integer>();
		for (String str : strList) {
			intList.add(Integer.parseInt(str));
		}
		return roleRepository.findAllById(intList);
	}

	/**
	 * Returns a {@link Page} of entities meeting the paging restriction provided in
	 * the {@code Pageable} object.
	 * 
	 * 分页获取数据
	 * 
	 * @param page
	 *            第几页
	 * @param limit
	 *            每页最长记录数
	 * @return a page of entities
	 */
	@RequestMapping(value = "/page", method = RequestMethod.GET)
	Page<Role> findAll(@RequestParam(value = "page", defaultValue = "0") Integer page,
			@RequestParam(value = "limit", defaultValue = "5") Integer limit) {
		Sort sort = new Sort(Direction.DESC, "roleId");
		Pageable pageable = new PageRequest(page, limit, sort);
		return roleRepository.findAll(pageable);
	}

	/**
	 * Flushes all pending changes to the database.
	 */
	@RequestMapping(value = "/flush", method = RequestMethod.GET)
	void flush() {
		roleRepository.flush();
	}
}